package com.whq.thrift.api.web.admin.controller.bs.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.admin.PermissionUri;
import com.whq.thrift.api.web.admin.db.admin.UserInfo;
import com.whq.thrift.api.web.admin.db.admin.UserMermission;
import com.whq.thrift.api.web.admin.service.admin.UserInfoService;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class UserInfoController {
	private static final int DEFAULT_PAGE_SIZE = 25;

	@Autowired
	private UserInfoService userInfoService;

	@RequestMapping(value = "bs/admin/userinfo/list")
	public String list(@RequestParam(value = "email", required = false) String email,
			@RequestParam(value = "currentPage", required = true, defaultValue = "1") Integer currentPage,
			@RequestParam(value = "pageSize", required = false) Integer pageSize, Model model) {
		PageData<UserInfo> pageData = new PageData<UserInfo>();
		pageData.setCurrentPage(currentPage);
		if ((pageSize == null) || (pageSize <= 0)) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		pageData.setPageSize(pageSize);

		// 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);
		if (StringUtil.isNotBlank(email)) {
			whereParams.put("email", email);
		}
		int count = this.userInfoService.findRecordCount(whereParams);
		pageData.setTotalCount(count);

		int beginIndex = (currentPage - 1) * pageSize;
		List<UserInfo> resultList = this.userInfoService.findRecordsByMap(whereParams, beginIndex, pageSize);
		pageData.setResultList(resultList);

		model.addAttribute("email", email);
		model.addAttribute("pageData", pageData);

		return "bs/admin/userinfo/list";
	}

	@RequestMapping(value = "bs/admin/userinfo/permissionlist")
	public String permissionlist(@RequestParam(value = "id", required = true) String email, Model model) {
		if (email == null) {
			return "error";
		}

		List<UserMermission> userList = this.userInfoService.findUserMermissionByEmail(email);

		List<PermissionUri> resultList = this.userInfoService.findPermissionByMap();

		List<Integer> tempList = new ArrayList<Integer>();
		for (int i = 0; i < userList.size(); i++) {
			tempList.add(userList.get(i).getPermissionId());
		}

		// 比较如果有相同的permissionid则checked设为true
		if (userList != null) {
			for (PermissionUri pUri : resultList) {
				if (tempList.contains(pUri.getPermissionId())) {
					pUri.setChecked(true);
				} else {
					pUri.setChecked(false);
				}
			}
		}

		model.addAttribute("email", email);
		model.addAttribute("record", resultList);
		return "bs/admin/userinfo/permissionlist";
	}

	@RequestMapping(value = "bs/admin/userinfo/show")
	public String show(@RequestParam(value = "id", required = true) String id, Model model) {
		if (id == null) {
			return "error";
		}
		UserInfo record = this.userInfoService.findRecordById(id);

		model.addAttribute("record", record);
		if (record == null) {
			return "error";
		}
		return "bs/admin/userinfo/show";
	}

	@RequestMapping(value = "bs/admin/userinfo/showAdd")
	public String showAdd(Model model) {
		UserInfo record = new UserInfo();
		model.addAttribute("record", record);
		return "bs/admin/userinfo/showAdd";
	}

	@RequestMapping(value = "bs/admin/userinfo/add")
	public String add(@Valid UserInfo record, BindingResult result, Model model) {
		UserInfo existRecord = this.userInfoService.findRecordById(record.getEmail());
		if (existRecord != null) {
			model.addAttribute("errMsg", "email已存在，不能重复！！");
			model.addAttribute("record", record);
			return "bs/admin/userinfo/showAdd";
		}

		this.userInfoService.addRecord(record);
		return "redirect:show.do?id=" + record.getEmail();
	}

	@RequestMapping(value = "bs/admin/userinfo/showUpd")
	public String showUpd(@RequestParam(value = "id", required = true) String id, Model model) {
		if (id == null) {
			return "error";
		}
		UserInfo record = this.userInfoService.findRecordById(id);
		if (record == null) {
			return "error";
		}
		model.addAttribute("record", record);
		return "bs/admin/userinfo/showUpd";
	}

	@RequestMapping(value = "bs/admin/userinfo/upd")
	public String upd(@Valid UserInfo record, BindingResult result, Model model) {
		if (result.hasErrors()) {
			model.addAttribute("record", record);
			return "bs/admin/userinfo/showUpd";
		}

		this.userInfoService.updateRecord(record);
		return "redirect:show.do?id=" + record.getEmail();
	}

	@RequestMapping(value = "bs/admin/userinfo/del")
	public String del(@RequestParam(value = "id", required = true, defaultValue = "0") String id, Model model) {
		this.userInfoService.deleteRecord(id);
		return "bs/admin/userinfo/delOk";
	}

	@RequestMapping(value = "bs/admin/userinfo/delRecords")
	public String delRecords(String[] delIds, Model model) {
		if (delIds != null) {
			for (int i = 0; i < delIds.length; i++) {
				if (delIds[i] != null) {
					this.userInfoService.deleteRecord(delIds[i]);
				}
			}
		}
		return "redirect:list.do";
	}

	@RequestMapping(value = "bs/admin/userinfo/updUserMerssion.do")
	public String updUserMerssion(String[] updEmails, @RequestParam(value = "email", required = false) String email, Model model) {
		this.userInfoService.delUserPermission(email);
		List<UserMermission> userList = this.userInfoService.findUserMermissionByEmail(email);
		if (userList != null) {
			model.addAttribute("errMsg", "权限已存在");
		}

		if (updEmails != null) {
			for (int i = 0; i < updEmails.length; i++) {
				UserMermission record = new UserMermission();
				record.setEmail(email);
				record.setPermissionId(Integer.valueOf(updEmails[i]));
				if (record != null) {
					this.userInfoService.addUserMermission(record);
				}
			}
		}else{
			this.userInfoService.delUserPermission(email);
		}
		return "redirect:permissionlist.do?id="+email;
	}
}
